package com.yuan.zs.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.image.BaseImageLoad;
import com.yuan.base.utils.ImageUtil;
import com.yuan.zs.R;
import com.yuan.zs.bean.AttachFile;
import com.yuan.zs.common.photoView.PhotoView;

import java.util.UUID;
import java.util.concurrent.ExecutionException;


/**
 * 大图展示
 * @version 1.0
 */
public class ShowImageActivity extends BaseFrameworkActivity {
	// --------------------------控件--------------------------
	private PhotoView image;
	// --------------------------变量--------------------------
	private AttachFile gridImg;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_show_image;
	}

	@Override
	protected void bindViews() {
		image = findViewById(R.id.image);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		gridImg = getIntent().getParcelableExtra("gridImg");

		BaseImageLoad.load(gridImg.getId(), image);
		image.enable();
		image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		image.setOnClickListener(v -> closeSelf());

		setOnClickListener(R.id.tv_save_camera, v -> {
			new Thread(() -> {
				try {
					Bitmap bitmap = BaseImageLoad.loadAndGet(gridImg.getId(), image);
					runOnUiThread(() -> ImageUtil.saveBmp2Gallery(bitmap, UUID.randomUUID().toString()));
				} catch (ExecutionException | InterruptedException e) {
					e.printStackTrace();
				}
			}).start();
		});

	}

	@Override
	public void closeSelf() {
		super.closeSelf();
		overridePendingTransition(0, R.anim.base_image_out);
	}

	public static void showImgById(Context context, String imageId) {
		if (TextUtils.isEmpty(imageId)) {
			return;
		}
		AttachFile svAttachFile = new AttachFile();
		svAttachFile.setId(imageId);
		Intent intent = new Intent(context, ShowImageActivity.class);
		intent.putExtra("gridImg", svAttachFile);
		context.startActivity(intent);
	}


}
